package com.seafood.entity;

import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.*;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单实体类
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "orders")
public class Order extends BaseEntity {
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", nullable = false)
    private User user;
    
    @NotBlank(message = "订单号不能为空")
    @Size(max = 50, message = "订单号长度不能超过50字符")
    @Column(name = "order_number", unique = true, nullable = false, length = 50)
    private String orderNumber;
    
    @NotNull(message = "总金额不能为空")
    @DecimalMin(value = "0.0", message = "总金额不能小于0")
    @Column(name = "total_amount", nullable = false, precision = 10, scale = 2)
    private BigDecimal totalAmount;
    
    @DecimalMin(value = "0.0", message = "运费不能小于0")
    @Column(name = "shipping_fee", precision = 10, scale = 2)
    private BigDecimal shippingFee = BigDecimal.ZERO;
    
    @DecimalMin(value = "0.0", message = "优惠金额不能小于0")
    @Column(name = "discount_amount", precision = 10, scale = 2)
    private BigDecimal discountAmount = BigDecimal.ZERO;
    
    @NotNull(message = "实付金额不能为空")
    @DecimalMin(value = "0.0", message = "实付金额不能小于0")
    @Column(name = "final_amount", nullable = false, precision = 10, scale = 2)
    private BigDecimal finalAmount;
    
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private OrderStatus status = OrderStatus.PENDING;
    
    @Column(name = "payment_method", length = 50)
    private String paymentMethod;
    
    @Column(name = "payment_time")
    private LocalDateTime paymentTime;
    
    @Lob
    @Column(name = "shipping_address")
    private String shippingAddress;
    
    @Column(name = "recipient_name", length = 100)
    private String recipientName;
    
    @Column(name = "recipient_phone", length = 20)
    private String recipientPhone;
    
    @Column(name = "logistics_company", length = 100)
    private String logisticsCompany;
    
    @Column(name = "tracking_number", length = 100)
    private String trackingNumber;
    
    @Lob
    private String remark;
    
    // 订单详情
    @OneToMany(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<OrderItem> orderItems;
    
    /**
     * 订单状态枚举
     */
    public enum OrderStatus {
        PENDING,     // 待支付
        PAID,        // 已支付
        SHIPPED,     // 已发货
        DELIVERED,   // 已送达
        CANCELLED    // 已取消
    }
}